package com.itheima.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.project.constant.ShopConstant;
import com.itheima.project.constant.trade.TradingConstant;
import com.itheima.project.mapper.OrderMapper;
import com.itheima.project.pojo.Order;
import com.itheima.project.pojo.Table;
import com.itheima.project.service.IOrderService;
import com.itheima.project.service.ITableService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.EmptyUtil;
import com.itheima.project.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description： 服务实现类
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    ITableService tableService;

    @Override
    public OrderVo findOrderByTableId(Long tableId) {
        // 根据桌台ID 根据订单状态  查询订单
        LambdaQueryWrapper<Order> wrapper = Wrappers.<Order>lambdaQuery();
        wrapper.eq(Order::getTableId,tableId);
        wrapper.eq(Order::getEnableFlag,ShopConstant.YES);
        wrapper.and(subWrapper->
            subWrapper.eq(Order::getOrderState,TradingConstant.DFK)
                      .or()
                      .eq(Order::getOrderState,TradingConstant.FKZ)
        );
        Order order = getOne(wrapper);
        return BeanConv.toBean(order,OrderVo.class);
//        LambdaQueryWrapper<Order> queryWrapper = Wrappers.<Order>lambdaQuery();
//        //1. 查询订单表  条件: 根据桌台id  订单状态:
//        queryWrapper.eq(Order::getTableId, tableId)
//                // 订单是否有效
//                .eq(Order::getEnableFlag, ShopConstant.YES)
//                // and ( 订单状态 = 待付款   or  订单状态 = 付款中  )
//                .and(wrapper ->
//                        wrapper.eq(Order::getOrderState, TradingConstant.DFK)
//                        .or()
//                        .eq(Order::getOrderState, TradingConstant.FKZ)
//                );
//
//        // 2. 根据条件拿到订单数据
//        Order order = getOne(queryWrapper);
//        // 3. 将得到的订单数据装维 OrderVo
//        return BeanConv.toBean(order,OrderVo.class);
    }

    @Override
    public OrderVo findOrderByOrderNo(Long orderNo) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Order::getOrderNo,orderNo);
        queryWrapper.lambda().eq(Order::getEnableFlag, ShopConstant.YES);
        queryWrapper.lambda().and(wrapper->wrapper
                .eq(Order::getOrderState,TradingConstant.DFK)
                .or().eq(Order::getOrderState,TradingConstant.FKZ));
        Order order = getOne(queryWrapper);
        return BeanConv.toBean(order,OrderVo.class);
    }

    @Override
    public Boolean rotaryTable(Long sourceTableId, Table targetTable, Long orderNo) {
        Order order = Order.builder()
                .tableId(targetTable.getId())
                .tableName(targetTable.getTableName())
                .areaId(targetTable.getAreaId()).build();
        //订单修改
        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Order::getTableId,sourceTableId).eq(Order::getOrderNo,orderNo);
        lambdaQueryWrapper.eq(Order::getOrderState,TradingConstant.DFK);
        return update(order,lambdaQueryWrapper);
    }

    @Override
    public Page<Order> findOrderVoPage(OrderVo orderVo, int pageNum, int pageSize) {
        Page<Order> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        if (!EmptyUtil.isNullOrEmpty(orderVo.getOrderNo())) {
            queryWrapper.lambda().eq(Order::getOrderNo,orderVo.getOrderNo());
        }
        if (!EmptyUtil.isNullOrEmpty(orderVo.getTableId())) {
            queryWrapper.lambda().eq(Order::getTableId,orderVo.getTableId());
        }
        if (!EmptyUtil.isNullOrEmpty(orderVo.getIsRefund())) {
            queryWrapper.lambda().eq(Order::getIsRefund,orderVo.getIsRefund());
        }
        if (!EmptyUtil.isNullOrEmpty(orderVo.getOrderState())) {
            queryWrapper.lambda().eq(Order::getOrderState,orderVo.getOrderState());
        }
        if (!EmptyUtil.isNullOrEmpty(orderVo.getTradingChannel())) {
            queryWrapper.lambda().eq(Order::getTradingChannel,orderVo.getTradingChannel());
        }
        queryWrapper.lambda().orderByDesc(Order::getCreatedTime);
        return page(page,queryWrapper);
    }

    @Override
    public OrderVo findOrderVoPaid(Long orderNo) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Order::getOrderNo,orderNo);
        queryWrapper.lambda().eq(Order::getEnableFlag, ShopConstant.YES);
        queryWrapper.lambda().eq(Order::getOrderState,TradingConstant.YJS);
        Order order = getOne(queryWrapper);
        return BeanConv.toBean(order,OrderVo.class);
    }

    @Override
    public List<OrderVo> findOrderVoPaying() {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Order::getEnableFlag, ShopConstant.YES);
        queryWrapper.lambda().eq(Order::getOrderState,TradingConstant.FKZ);
        List<Order> orderList = list(queryWrapper);
        return BeanConv.toBeanList(orderList,OrderVo.class);
    }

    @Override
    public Boolean updateOrderStateByOrderNo(Long orderNo, String orderState) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(Order::getOrderNo,orderNo);
        Order order = Order.builder().orderState(orderState).build();
        return update(order,updateWrapper);
    }

}
